package com.hydl.power.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.github.pagehelper.PageInfo;
import com.hydl.power.aop.annotation.LogAnnotation;
import com.hydl.power.mapper.LogMapper;
import com.hydl.power.pojo.Device;
import com.hydl.power.pojo.Log;
import com.hydl.power.service.LogService;
import com.hydl.power.utils.Resp;
import com.hydl.power.utils.TableUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;


@Api(value = "logController",tags = "日志控制类")
@RestController
@RequestMapping(value = "/log")
@CrossOrigin
    public class LogController {

    @Autowired
    private LogService logService;
    @Autowired
    private LogMapper logMapper;



    //@LogAnnotation(module = "日志控制类",description = "获取日志列表")
    @ApiOperation(value = "获取日志列表", notes = "获取日志列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "当前页", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "条数", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "startTime", value = "开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间", required = false, dataType = "String")
    })
    @PostMapping(value = "/getLogList")
    public  Resp<PageInfo<Log>> getLogList(Integer pageNum,Integer pageSize, String startTime, String endTime) {
        PageInfo<Log> list= logService.getLogList(pageNum, pageSize, startTime, endTime);
        return Resp.success(list);
    }










    @GetMapping(value = "/index")
    public ModelAndView index() {
        return new ModelAndView("system/log");
    }



   /* @LogAnnotation(module = "日志控制类",description = "获取日志列表")
    @ApiOperation(value = "获取日志列表", notes = "根据起止时间获取日志列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "页尺寸", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "当前页", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "startTime", value = "开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间", required = false, dataType = "String")
    })
    @GetMapping(value = "/selectlog")
    public TableUtils selectAllByConditions(@RequestParam(value = "pageSize", required = true) Integer pageSize,
                                            @RequestParam(value = "pageNum", required = true) Integer pageNum,
                                            @RequestParam(value = "startTime", required = false) String startTime,
                                            @RequestParam(value = "endTime", required = false) String endTime) {

        return logService.selectAllByConditions(pageSize, pageNum, startTime, endTime);
    }*/

    @LogAnnotation(module = "日志控制类",description = "日志导出")
    @ApiOperation(value = "日志导出")
    @GetMapping(value = "/logExport")
    public void logExport(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("操作日志表", "UTF-8") + ".xls");
        //        //编码
        response.setCharacterEncoding("UTF-8");

        List list = logMapper.selectAll();
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("操作日志", "操作历史"),
                Log.class, list);

        workbook.write(response.getOutputStream());

    }


}
